package com.welink.cmp.adapter.web;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.util.StringUtils;
import com.welink.base.catchlog.CatchAndLog;
import com.welink.base.exception.BizException;
import com.welink.cmp.dto.ErrorCode;
import com.welink.cmp.dto.metadata.ExcelImport;
import com.welink.cmp.meatdata.importdata.*;
import com.welink.cmp.meatdata.service.ExcelImportHandleService;
import com.welink.common.dto.SingleResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @author yanwei li create  2024-06-18 12:09
 */
@RestController("/import/excel")
@CatchAndLog
@Slf4j
public class AssetsExcelImportController {

    @Autowired
    private FirewallExcelListener firewallExcelListener;
    @Autowired
    private SwitchExcelListener switchExcelListener;
    @Autowired
    private EipExcelListener eipExcelListener;
    @Autowired
    private HostExcelListener hostExcelListener;
    @Autowired
    private RouterExcelListener routerExcelListener;

    @PostMapping("/upload")
    public SingleResponse<String> uploadExcelFile(@RequestParam("file") MultipartFile file, @RequestParam(value = "sheetName", required = false) String sheetName) {
        if (file == null || file.isEmpty()) {
            return SingleResponse.buildFailure(ErrorCode.CommonError.E_Service_Param_Exception,"上传文件为空");
        }
        String result = null;
        try {
            sheetName = sheetName.replace(",","");
            result = readExcel(file, sheetName);
            return SingleResponse.of("上传文件成功，明细如下：" + result);
        } catch (Exception e) {
            return SingleResponse.of("上传文件异常: "+e.getMessage());
        }
    }



    private String readExcel(MultipartFile file,String sheetName){


        try{
            if (StringUtils.isBlank(sheetName) || sheetName.contains("firewall")){
                ExcelReaderBuilder readerBuilder = EasyExcel.read(file.getInputStream(),FirewallExcelColumn.class,new FirewallExcelListener());
                readerBuilder.sheet(sheetName).doRead();
            }
            if (StringUtils.isBlank(sheetName) || sheetName.contains("switch")){
                ExcelReaderBuilder readerBuilder = EasyExcel.read(file.getInputStream(), SwitchExcelColumn.class,switchExcelListener);
                final List<Object> objects = readerBuilder.sheet(sheetName).doReadSync();
            }
            if (StringUtils.isBlank(sheetName) || sheetName.contains("eip")){
                ExcelReaderBuilder readerBuilder = EasyExcel.read(file.getInputStream(), EipExcelColumn.class, eipExcelListener);
                final List<Object> objects = readerBuilder.sheet(sheetName).doReadSync();
            }
            if (StringUtils.isBlank(sheetName) || sheetName.contains("host")){
                ExcelReaderBuilder readerBuilder = EasyExcel.read(file.getInputStream(),HostExcelColumn.class,hostExcelListener);
                final List<Object> objects = readerBuilder.sheet(sheetName).doReadSync();
            }
            if (StringUtils.isBlank(sheetName) || sheetName.contains("router")){
                ExcelReaderBuilder readerBuilder = EasyExcel.read(file.getInputStream(),RouterExcelColumn.class,routerExcelListener);
                final List<Object> objects = readerBuilder.sheet(sheetName).doReadSync();
            }
//            if (StringUtils.isBlank(sheetName) || sheetName.contains("vpc")){
//                ExcelReaderBuilder readerBuilder = EasyExcel.read(file.getInputStream(),FirewallExcelColumn.class,new FirewallExcelListener());
//                final List<Object> objects = readerBuilder.sheet(sheetName).doReadSync();
//            }
        }catch (IOException ioException){
            throw new BizException("读取文件异常：",ioException);
        }
        return "读取数据并写入完毕";
    }

}
